package com.qiqu.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qiqu.Vo.ArticleVo;
import com.qiqu.Vo.CommentsVo;
import com.qiqu.Vo.UsersCollectionVo;
import com.qiqu.dao.ArticleDao;
import com.qiqu.dao.UsersCollectionDao;
import com.qiqu.dao.UsersDao;
import com.qiqu.dao.UsersLikeArticleDao;
import com.qiqu.dao.mapper.UsersCollectionCustom;
import com.qiqu.entity.Article;
import com.qiqu.entity.UsersCollection;
import com.qiqu.entity.UsersLikeArticle;
import com.qiqu.service.UsersCollectionService;
import com.qiqu.service.UsersLikeArticleService;
import com.qiqu.utils.PagedResult;
import com.qiqu.utils.TimeAgoUtils;
import org.n3r.idworker.Sid;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


@Service("usersCollectionService")
public class UsersCollectionServiceImpl implements UsersCollectionService {
    @Resource
    private UsersCollectionDao usersCollectionDao;
    @Resource
    private Sid sid;
    @Resource
    private ArticleDao  articleDao;
    @Resource
    private UsersDao usersDao;
    @Resource
    private UsersCollectionCustom usersCollectionCustom;


    @Override
    public boolean collection(String userId, String articleId) {
        UsersCollection usersCollection= new UsersCollection(sid.nextShort(), userId, articleId,1,new Date());
        //添加关注数
        Article article1=new Article();
        article1.setId(articleId);
        Article article = articleDao.selectOne(article1);
        usersDao.addReceiveLikeCounts(article.getUserId());

        // 文章喜欢数量加一
        articleDao.addCount(articleId);
        return usersCollectionDao.insert(usersCollection) > 0;
    }

    @Override
    public boolean cancleCollection(String userId, String articleId) {
        Article article1=new Article();
        article1.setId(articleId);
        Article article = articleDao.selectOne(article1);
        usersDao.subReceiveLikeCounts(article.getUserId());

        articleDao.subCount(articleId);
        Example c = new Example(UsersCollection.class);
        Example.Criteria criteria = c.createCriteria();
        criteria.andEqualTo("userId",userId);
        criteria.andEqualTo("articleId",articleId);
        return usersCollectionDao.deleteByExample(c)>0;
    }

    @Override
    public boolean isCollection(String userId, String articleId) {
        Example c = new Example(UsersCollection.class);
        Example.Criteria criteria = c.createCriteria();
        criteria.andEqualTo("userId",userId);
        criteria.andEqualTo("articleId",articleId);
        List<UsersCollection> usersCollection=usersCollectionDao.selectByExample(c);
        if(usersCollection==null || usersCollection.size()==0){
            return false;
        }
        return true;
    }

    @Override
    public int updateStatus(String userId) {

        return usersCollectionDao.updateStatus(userId);
    }

    @Override
    public int queryStatusCount(String userId) {
        return usersCollectionDao.queryStatusCount(userId).size();
    }

    @Override
    public PagedResult getCollection(Integer page, Integer pageSize, String userId) {
        PageHelper.startPage(page,pageSize);
        List<UsersCollectionVo> usersCollectionVos= usersCollectionCustom.queryList(userId);

        for(UsersCollectionVo usersCollectionVo:usersCollectionVos){
            usersCollectionVo.setTimeDesc(TimeAgoUtils.format(usersCollectionVo.getCreateTime()));
        }
        PageInfo<UsersCollectionVo> pageList=new PageInfo<>(usersCollectionVos);
        PagedResult pagedResult =new PagedResult();
        pagedResult.setPage(page);
        pagedResult.setTotal(pageList.getPages());
        pagedResult.setRows(usersCollectionVos);
        pagedResult.setRecords(pageList.getTotal());
        return pagedResult;

    }


}
